﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using UI.Lib;
using Model;
using Telerik.Reporting;
using Components;
using UI.Lib.Factory;
using Com.Seezt.Skins;
using UI.Lib.Controls;
using Telerik.Reporting.Drawing;

namespace UI.ParentForm
{
    /// <summary>
    /// 打印的父级页面
    /// </summary>
    public partial class BasePrint : BaseForm
    {
        #region 参数集合

        private string _pageName;
        //加载报表的名字
        public string PageName
        {
            get { return _pageName; }
        }

        //当前页面的参数集合
        private Dictionary<string, string> param = new Dictionary<string, string>();

        #endregion

        private bool _isReportOk = true;

        public bool IsReportOk
        {
            get { return _isReportOk; }
            set { _isReportOk = value; }
        }

        private string _reportType;

        public string ReportType
        {
            get { return _reportType; }
            set { _reportType = value; }
        }

        public BasePrint()
        {
            InitializeComponent();

        }

        #region 参数相关
        /// <summary>
        /// 设置页面参数
        /// </summary>
        /// <param name="p"></param>
        /// <param name="v"></param>
        public void AddParam(string p, string v)
        {
            param.Add(p, v);
        }

        protected string GetParamByKey(string key)
        {
            return (param.ContainsKey(key)) ? param[key] : null;
        }

        public void AddParam(params TextValuePair[] v)
        {
            foreach (TextValuePair item in v)
            {
                param.Add(item.Text, item.Value);
            }
        }
        #endregion

        private void repViewMain_Load(object sender, EventArgs e)
        {

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _pageInit();

            this.repViewMain.RefreshReport();
        }

        protected virtual void SetParam()
        {
        
        }

        protected void _pageInit()
        {
            SetParam();

            Hashtable where = new Hashtable();
            where["comcode"] = Context.Comocde;
            where["@pagename"] = this.GetType().FullName;
            where["@rptype"] = string.IsNullOrEmpty(this.ReportType) ? "" : this.ReportType;

            //读取报表的相关信息
            DataTable dtReport = CommonHelper.Entity.ExecFun("getprintparam", where);
            if (DataTableHelper.Entity.IsEmptyTable(dtReport))
            {
                MsgBox.ShowErrorMsg(this, "未能加载报表的相关设置！");
                return;
            }

            where = null;
            _pageName = Validation.ToString(dtReport.Rows[0]["pagename"]); //打印页面的Url
            double width = Validation.ToDouble(dtReport.Rows[0]["value1"]); //页面宽度
            double height = Validation.ToDouble(dtReport.Rows[0]["value2"]); //页面高度

            Telerik.Reporting.Report rep = System.Reflection.Assembly.Load("UI").CreateInstance(_pageName, true) as Report;
            if (null == rep)
            {
                MessageBox.Show("当前打印报表模块不存在，打印失败！", "提示");
                return;
            }

            //设置报表的宽度
            rep.Width = new Telerik.Reporting.Drawing.Unit(width / 10, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm)));
            rep.Width = rep.Width - rep.PageSettings.Margins.Left - rep.PageSettings.Margins.Right;
            rep.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.Custom;
            rep.PageSettings.PaperSize = new SizeU(new Telerik.Reporting.Drawing.Unit(width / 10, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))),
                new Telerik.Reporting.Drawing.Unit(height / 10, ((Telerik.Reporting.Drawing.UnitType)(Telerik.Reporting.Drawing.UnitType.Cm))));

            Hashtable values = ConstValues();
            //设置公用参数
            ReportParameterCollection ps = rep.ReportParameters;
            foreach (var p in ps)
            {
                if (values.ContainsKey(p.Name))
                {
                    rep.ReportParameters[p.Name].Value = Validation.ToString(values[p.Name]);
                }
            }

            //设置页面大小

            //设置报表名称
            this.repViewMain.Report = rep;

            //设置概要和明细的值（暂略）
            DataTable dtSummary = SetReportSummaryData();
            if (!DataTableHelper.Entity.IsEmptyTable(dtSummary))
            {
                DataRow data = dtSummary.Rows[0];
                ReportHeaderSection reportHeader = rep.Items.FindItem(typeof(Telerik.Reporting.ReportHeaderSection)) as ReportHeaderSection;
                if (null != reportHeader)
                {
                    DataColumnCollection dcc = dtSummary.Columns;
                    ReportTextbox box = null;
                    foreach (ReportItemBase item in reportHeader.Items)
                    {
                        if (!dcc.Contains(item.Name)) continue;
                        if (item is ReportTextbox)
                        {
                            box = (ReportTextbox)item;
                            box.Value += Validation.ToString(data[item.Name]);
                        }
                    }
                }

                //应急情况下，写的冗余了点
                PageHeaderSection pageHeader = rep.Items.FindItem(typeof(Telerik.Reporting.PageHeaderSection)) as PageHeaderSection;
                if (null != pageHeader)
                {
                    DataColumnCollection dcc = dtSummary.Columns;
                    ReportTextbox box = null;
                    foreach (ReportItemBase item in pageHeader.Items)
                    {
                        if (!dcc.Contains(item.Name)) continue;
                        if (item is ReportTextbox)
                        {
                            box = (ReportTextbox)item;
                            box.Value += Validation.ToString(data[item.Name]);
                        }
                    }
                }
            }

            DataTable dtDetail = SetReportDetailData();
            rep.DataSource = dtDetail; //设置报表的明细数据源

            AfterReport();
        }

        protected virtual void  AfterReport() { }

        /// <summary>
        /// 设置报表明细的数据源
        /// </summary>
        /// <returns></returns>
        protected virtual DataTable SetReportDetailData() { return null; }

        /// <summary>
        /// 设置报表概要部分的数据源
        /// </summary>
        /// <returns></returns>
        protected virtual DataTable SetReportSummaryData() { return null; }

        /// <summary>
        /// 设置报表名称
        /// </summary>
        protected virtual Telerik.Reporting.Report SetReportName() { return null; }


        private Hashtable ConstValues()
        {
            Hashtable values = new Hashtable();
            values["COMCODE"] = Context.Comocde;
            values["COMNAME"] = Context.ComoName;
            values["USERCODE"] = Context.LoginName;
            values["USERNAME"] = Context.User.Empname;

            Psn0a00 psn = Context.CompanyInfo;
            if (null == psn) return values;

            values["COMTEL"] = psn.Ntel; //公司电话
            values["COMFAX"] = psn.Fax; //公司传真
            values["COMADDRESS"] = psn.Address;

            return values;
        }
    }
}
